﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ACompany.OrderProcessing.Model
{
    public class Address
    {
        // it is a value object class

        private string _street;
        public string Street
        {
            get { return _street; }
            private set { _street = value; }
        }

        private string _city;
        public string City
        {
            get { return _city; }
            private set { _city = value; }
        }

        private string _country;
        public string Country
        {
            get { return _country; }
            private set { _country = value; }
        }

        private string _postCode;
        public string PostCode
        {
            get { return _postCode; }
            private set { _postCode = value; }
        }


        private Address()
        {
        }

        public Address(string street, string city, string country, string postCode)
        {
            this.Street = street;
            this.City = city;
            this.Country = country;
            this.PostCode = postCode;
            this.Validate();
        }
        protected void Validate()
        {
            StringBuilder sb = new StringBuilder();

            if (string.IsNullOrEmpty(Street))
                sb.AppendLine("Street is a required value");
            else if (Street.Length > 50)
                sb.AppendLine("Street should be 50 chars or less");

            if (string.IsNullOrEmpty(Country))
                sb.AppendLine("Country is a required value");
            else if (Country.Length > 50)
                sb.AppendLine("Country should be 50 chars or less");

            if (string.IsNullOrEmpty(City))
                sb.AppendLine("City is a required value");
            else if (City.Length > 50)
                sb.AppendLine("City should be 50 chars or less");

            if (string.IsNullOrEmpty(PostCode))
                sb.AppendLine("PostCode is a required value");
            else if (PostCode.Length > 50)
                sb.AppendLine("PostCode should be 50 chars or less");

            if (!string.IsNullOrEmpty(sb.ToString()))
            {
                throw new ArgumentException(string.Format("Broken rules on address, please correct the following errors: {0}{1}", Environment.NewLine, sb.ToString()));
            }

        }
    }
}
